package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.ReservationDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.entity.OrderSetting;
import com.itheima.health.service.ReservationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class ReservationServiceImpl implements ReservationService {
    @Autowired
    private ReservationDao reservationDao;

    /**
     * 预约管理上传文件_当月预约设置
     *
     * @param file
     * @return
     */
    @Override
    public void processExcelFile(MultipartFile file) {
        try {
            XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
            XSSFSheet sheetAt = workbook.getSheetAt(0);

            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (Row row : sheetAt) {
                if (row.getRowNum() == 0) continue;
                if (row == null) return;

                Date orderDate = row.getCell(0).getDateCellValue();
                int number = (int) row.getCell(1).getNumericCellValue();

                OrderSetting setting = new OrderSetting(orderDate, number);

                orderSettingList.add(setting);
            }
            if (orderSettingList != null && !orderSettingList.isEmpty()) {
                for (OrderSetting orderSetting : orderSettingList) {
                    reservationDao.processExcelFile(orderSetting);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 当月预约信息展示
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public List<Map<String, Object>> getOrderSettingByMonth(String year, String month) {
        String date = year + "-" + month + "-" + 01;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date orderDate = dateFormat.parse(date);
            List<OrderSetting> orderSettingList = reservationDao.getOrderSettingByMonth(orderDate);

            List<Map<String, Object>> fuck = new ArrayList<>();
            for (OrderSetting orderSetting : orderSettingList) {
                Map<String, Object> map = new HashMap<>();
                String[] split = orderSetting.getOrderDate().toString().split(" ");
                String s = split[2];
                map.put("date", s);
                map.put("number", orderSetting.getNumber());
                map.put("reservations", orderSetting.getReservations());
                fuck.add(map);
            }

            return fuck;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 预约管理可预约单独设置
     *
     * @param orderSetting
     * @return
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        int getNumber = reservationDao.fuckingSearchFor(orderSetting.getOrderDate());
        if (getNumber < orderSetting.getNumber()) {
            throw new BusinessRuntimeException(MessageConst.ORDERSETTING_FAIL);
        }
        reservationDao.updateFuking(orderSetting);
    }
}
